{% extends "base.html" %}

{% block css %}
<link rel="stylesheet" href="/static/css/admin.css" />
{% endblock %}
{% block nav %}
   <div class="navbar navbar-fixed-top">
    <div class="navbar-inner">
      <div class="container">

        <a class="btn btn-navbar" data-toggle="collapse" data-target=".nav-collapse">
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </a>
        <a class="brand" href="/admin/">Googler Admin</a>
        <div class="nav-collapse">
          <ul class="nav">

            <li class="active"><a href="/admin/">Home</a></li>
            <li><a href="/admin/view_companies">Companies</a></li>
          </ul>
        </div>
      </div>
    </div>
  </div>
{% endblock %}
{% block content %}
<div  class="alert" style="display:none">

</div>
<form id="partnerForm" class="form-horizontal" method="POST" onsubmit="return submitForm(this)" action="/admin/associate_partners" >
<fieldset>
  <legend>Create Curation Organization</legend>
    <div class="control-group">
      <label for="select01" class="control-label">Select An Organization</label>
      <div class="controls">
      <select onchange="updatePreselect(this.value)" name="organization" id="organizations">
      {% for org in organizations %}
        <option value="{{org.key}}">{{org.name}}</option>
      {% endfor %}
      </select>
      </div>
    </div>
    <div class="control-group">
      <label for="multiSelect" class="control-label">Associate Partners</label>
      <div class="controls">
        <p  id="multiPartnerSelect">
        {% for uploaders in partners %}
        <input type="checkbox" onclick="updateAssociation(this)" name="partners" value="{{uploaders.yt_username}}" {% for preselect in preselect_partners_for_default_org %} {% ifequal preselect.uploader uploaders.yt_username %} checked="checked" {% endifequal %} {% endfor %} />{{uploaders.yt_username}}
        <br />
        {% endfor %}
       </p>
      </div>
    </div>
    <div class="form-actions">
     <input class="btn btn-primary" value="Update partners in the curation tool NOW" type="submit"/>
    </div>
</fieldset>
</form>
{% endblock %}
 
{% block foot_js_lab %}
  function updateAssociation(checkbox){
    if (checkbox.checked){
      var action = 'add';
    } else {
      var action = 'delete';
    }

    $.ajax({
      url: $("#partnerForm").attr('action'),
      type: $("#partnerForm").attr('method'),
      dataType: "json",
      data: $("#partnerForm").serialize() + '&action=' + action + '&uploader=' + checkbox.value,
      success: function(message) {
        $(".alert").html(message['message']);
        $(".alert").css("display", "block");
      },
    });
  }
  function submitForm(form){
    $.ajax({
      url: form.action,
      type: form.method,
      dataType: "json",
      data: $("#partnerForm").serialize() + '&action=submit',
      success: function(message) {
        $(".alert").html(message['message']);
        $(".alert").css("display", "block");
      },
    });
    return false;
  }
  function updatePreselect(org_key){
    $.ajax({
      url: "/admin/getorgpartners?org_key=" + org_key,
      type: "get",
      dataType: "json",
      success: function(message) {
      if (message.length == 0 ) {
        $('#multiPartnerSelect input').removeAttr('checked');
        
      } else {
      $('#multiPartnerSelect').find('input').each(function() {
          var selectOption = $(this);
          var found = false;
          $.each(message, function(i, preselectPartner){
            console.log(preselectPartner);
            if (preselectPartner == selectOption.val()) {
              selectOption.attr('checked','checked');
              found = true;
            }
          });
          if (!found) {
            selectOption.removeAttr('checked');
          }
          
        });
      }
      },
    });
  }

{% endblock %}
